package com.wef.system.service.impl;

import java.util.List;
import java.util.Set;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.AclCondition;
import com.wef.system.service.AclConditionService;

public class AclConditionServiceImpl extends BaseServiceImpl implements AclConditionService {

	public AclCondition addAclCondition(AclCondition aclCondition){
		
		return (AclCondition) super.saveObject(aclCondition);
	}

	public void deleteAclCondition(AclCondition aclCondition) {		
		super.deleteObject(aclCondition);
		
	}

	public AclCondition saveOrUpdateAclCondition(AclCondition aclCondition){	
		if (aclCondition.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			return this.addAclCondition(aclCondition);
		}
		if (aclCondition.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			return this.updateAclCondition(aclCondition);
		}
		if (aclCondition.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			this.deleteAclCondition(aclCondition);
			return null ;
		}
		return aclCondition;
	}

	public Set<AclCondition> saveOrUpdateAclConditions(Set<AclCondition> aclConditions){
		StringBuilder errorString = new StringBuilder();
		for (AclCondition aclCondition : aclConditions) {
			try {
				saveOrUpdateAclCondition(aclCondition);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
		return aclConditions;
	}

	public AclCondition updateAclCondition(AclCondition aclCondition) {
		return (AclCondition) super.updateObject(aclCondition);
	}	
	

}
